<!--
	作者：offline
	时间：2018-10-30
	描述：会计对账审批列表
-->
<template>
	<div class="m-view-goodsAccountBill m-window">
		<div class="m-condition" v-if="diaShow">
			<el-row :gutter="10">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					:label-width="labelWidth"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="7">
						<el-form-item :label="label" class="rightItem">
							<el-select
								value-key="id"
								v-model="selCompanyCode"
								:placeholder="placeholder"
								@change="companyChange"
								clearable
								ref="content"
								:disabled="btnDis"
								filterable
							>
								<el-option
									v-for="item in comOptions"
									:key="item.id"
									:label="item.companyName"
									:value="item"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="申请日期" class="rightItem">
							<el-date-picker
								v-model="applySDate"
								type="date"
								:placeholder="placeholderDate.start"
								value-format="yyyy-MM-dd"
								:editable="false"
								:picker-options="po1"
							></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-date-picker
							v-model="applyEDate"
							type="date"
							:placeholder="placeholderDate.end"
							value-format="yyyy-MM-dd"
							:editable="false"
							:picker-options="po2"
						></el-date-picker>
					</el-col>
					<el-col :span="6">
						<el-form-item label="对账单号" class="rightItem">
							<el-input v-model="accountNo" placeholder="请输入单号" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<el-row :gutter="10">
				<el-form
					:model="ruleForm"
					ref="ruleForm"
					label-width="70px"
					:inline-message="true"
					label-position="left"
				>
					<el-col :span="7" v-if="!btnShow">
						<el-form-item label="供应商" class="rightItem">
							<el-input
								placeholder="请选择供应商"
								v-model="selCusName"
								clearable
								readonly
								@focus="showSupplierDialog"
								@mouseenter.native="mouseEnter"
								@mouseleave.native="mounseLeave"
								:disabled="btnDis"
							>
								<el-button slot="append" icon="el-icon-search" @click="showSupplierDialog"></el-button>
								<span slot="suffix" @click="inputClearSon" class="m-cleanInput" v-show="inputClearBut">
									<i class="el-input_icon el-icon-circle-close el-input__clear"></i>
								</span>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="单据状态" class="rightItem">
							<el-select v-model="recCode" placeholder="单据状态" clearable @change="recCode_change">
								<el-option
									v-for="item in recOptions"
									:key="item.value"
									:label="item.label"
									:value="item.value"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="5">
						<el-form-item label="签署模式" class="rightItem">
							<el-select v-model="signModel" placeholder="签署模式" clearable @change="recCode_change">
								<el-option
									v-for="item in modelOptions"
									:key="item.value"
									:label="item.label"
									:value="item.value"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-form>
				<el-col :span="2" :offset="!btnShow?4:11">
					<el-button type="primary" @click="search(1)" :disabled="btnDis">查询</el-button>
				</el-col>
				<el-col :span="2" v-if="auditBtn">
					<el-button type="primary" @click="auditAccountData" :disabled="btnDis">审核</el-button>
				</el-col>
			</el-row>
		</div>
		<div class="m-agTable m-bottom m-top2" v-if="diaShow">
			<accountAuditCheckInfo ref="getAccountInfo" @getClickValue="getClickValue"></accountAuditCheckInfo>
		</div>
		<div class="m-page" v-if="diaShow">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="currentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>

		<el-dialog
			title="供应商信息"
			:visible.sync="srmSupplierDialogVisible"
			width="50%"
			:before-close="srmSupplierDialogClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<srmSupplierInfo ref="srmSupplierInfo" :layout="layout" @setSrmSupplierInfo="setSrmSupplierInfo"></srmSupplierInfo>
			<!--@setSapSupplierCompanyInfo="getSelectedCompany" :setSelectedSupplierCode="getSelectedSupplierCode"-->
		</el-dialog>

		<!--新增会计对账-->
		<div class="m-window" v-if="!diaShow">
			<accountAuditComponents
				@cancelComponents="cancelComponents"
				:pageLayOut="pageLayOut"
				:authInfo="authInfo"
				:setClickValue="setClickValue"
			></accountAuditComponents>
		</div>
	</div>
</template>

<script>
import _Api from "@/api";
//	import localeText from "@/i18n/agGridText";
import accountAuditCheckInfo from "@/views/accountManagement/accountAuditCheckInfo.vue"
//	import orderInfo from "@/views/accountManagement/orderInfo.vue"
//	import storageInfo from "@/views/accountManagement/storageInfo.vue"
import accountAuditComponents from "@/views/accountManagement/accountAuditComponents.vue";
import srmSupplierInfo from "@/components/srmSupplierInfo.vue";
import constant from "@/public/constant";
import serviceTime from "@/public/serverTime.js";
import Msg from "@/public/message.js";

export default {
	data () {
		return {
			label: "",
			labelWidth: '70px',
			ruleForm: {},
			selCusCode: "",
			selCusName: "",
			cusOptions: [],
			selSupplierCode: "",
			supOptions: [],
			selCompanyCode: "",
			comOptions: [],
			accountNo: "",
			//jjc20181014
			applySDate: "",
			applyEDate: "",
			recCode: "",
			//单据状态
			recOptions: [],
			diaShow: true, //控制显示列表查询界面, 否则显示会计对账单详情界面
			btnShow: true, //控制显示供应商用户相关的控件，否则显示非供应商相关控件
			currentPage: 1,
			pageSize: 30,
			total: 0,
			//optionsShow: false,
			accountInfo: [],
			signCode: "",
			// //签署状态
			// signOptions:[
			// {	value:1,label:"未进行"},
			// {	value:2,label:"进行中"},
			// {	value:3,label:"已完成"}],
			placeholder: "",
			supplierInfo: "",
			//accShow:false,
			pageLayOut: "add",//页面配置，判断当前属于编辑/新增
			setClickValue: {},
			authInfo: [],
			auditBtn: false,//先不要批量审核
			btnDis: true,
			srmSupplierDialogVisible: false,
			layout: {},
			inputClearBut: false,
			po1: {
				disabledDate: this.disabledDate1
			},
			po2: {
				disabledDate: this.disabledDate2
			},
			current: 0,
			textChange: 0,
			// 日期控件默认值
			placeholderDate: {
				start: '',
				end: ''
			},
			// 签署模式下拉
			modelOptions: [
				{ value: 0, label: "线上" },
				{ value: 1, label: "线下" }
			],
			// 签署模式
			signModel: 0
		}
	},
	components: {
		accountAuditCheckInfo,
		accountAuditComponents,
		srmSupplierInfo
	},
	methods: {
		disabledDate1 (time) {
			if (this.applyEDate)
				return time.getTime() > new Date(this.applyEDate).getTime();
		},
		disabledDate2 (time) {
			if (this.applySDate)
				return time.getTime() < (new Date(this.applySDate).getTime() - 3600 * 1000 * 24);
		},
		//单据状态选择
		recCode_change () {
			this.current = 1;
			this.search();
		},
		showSupplierDialog () {
			var that = this;
			if (!this.layout.companyCode) {

				Msg.showPopMsg("提示：请选择公司！")
				this.$refs.content.focus();
				return;
			} else {
				this.srmSupplierDialogVisible = true;
				this.$nextTick(_ => {
					that.$refs.srmSupplierInfo.search();
				});
			}
		},
		mounseLeave () {
			if (this.selCusName != "")
				this.inputClearBut = false;
		},
		mouseEnter () {
			if (this.selCusName != "")
				this.inputClearBut = true;
		},
		inputClearSon () {
			this.selCusCode = "";
			this.selCusName = "";
			this.inputClearBut = false;
		},
		setSrmSupplierInfo (msg) {
			if (msg.length > 0) {
				this.srmSupplierDialogVisible = false;
				this.selCusCode = msg[0].id;
				this.selCusName = msg[0].supplierName;
				// this.inputClearBut = true;
			}
		},
		srmSupplierDialogClose (done) {
			this.$refs.srmSupplierInfo.keyWord = "";
			this.$refs.srmSupplierInfo.selSupplierOptions.api.setQuickFilter(this.$refs.srmSupplierInfo.keyWord);
			done();
		},
		companyChange (value) {
			//公司下拉框选择
			if (value) {
				this.layout.companyCode = value.companyCode;
				this.layout.auth = true;
			} else {
				this.layout = {};
			}
		},
		search (flag) {
			//查询方法
			var obj = {};
			if (this.selCompanyCode == "") {
				Msg.showPopMsg("提示：" + this.placeholder)
				return;
			}
			if (this.$global.user.userTypeId == "4") {
				obj.supplierId = constant.supplierId;
			} else {
				if (this.selCusCode != "") obj.supplierId = this.selCusCode;
			}
			if (this.selCompanyCode != "")
				obj.companyId = this.selCompanyCode.id;
			if (this.accountNo != "")
				obj.accountNo = this.accountNo;
			//jjc20181014
			if (this.applySDate != "" && this.applySDate != null) {
				obj.begin_date = this.applySDate;
				if (this.applyEDate == "" || this.applyEDate == null)
					obj.end_date = obj.begin_date;
			}
			if (this.applyEDate != "" && this.applyEDate != null) {
				obj.end_date = this.applyEDate;
				if (this.applySDate == "" || this.applySDate == null)
					obj.begin_date = obj.end_date;
			}
			if (obj.begin_date != "" && obj.end_date != "") {
				if (obj.begin_date > obj.end_date) {
					Msg.showPopMsg("提示：开始日期不能大于结束日期")
					return;
				}
			}
			//单据状态
			if (this.recCode == "" || !this.recCode)
				obj.billState = 0;
			else
				obj.billState = this.recCode;

			if (flag === 1)
				this.currentPage = 1;
			obj.current = this.currentPage;
			obj.size = this.pageSize;
			if (this.$global.user.userTypeId == "4")
				obj.userType = 2;
			else
				obj.userType = 1;
			obj.flag = 2;//会计对账审批页面
			obj.signMode = this.signModel
			var that = this;
            this.$refs.getAccountInfo.loading = true;
            this.$refs.getAccountInfo.setColumnDefs()
			_Api.getMyAccountList(obj).then(function (response) {
				var data = response.data;
				that.current = 0;
				that.textChange = 0;
				that.btnDis = false;
				that.$refs.getAccountInfo.loading = false;
				if (data.code == "1") {
					if (data.total > 0) {
						that.accountInfo = data.rows;
						that.total = data.total;
						that.$refs.getAccountInfo.getDataRows(that.accountInfo);
					} else {
						that.accountInfo = [];
						that.total = 0;
						that.$refs.getAccountInfo.getDataRows([]);
					}
				} else {
					that.total = 0;
					Msg.showErrorMsg(data.info);
				}
			}).catch((err) => {
				that.$refs.getAccountInfo.loading = false;
				that.$refs.getAccountInfo.getDataRows([]);
				that.total = 0;
				that.current = 0;
				that.textChange = 0;
				Msg.showErrorMsg(err.data.message);
			})
		},
		// getAuthInfo() {
		// 	//获取当前账号权限公司
		// 	var obj = {};
		// 	if(this.$global.user.userTypeId == 4) {
		// 		obj.account = this.$global.user.userName;
		// 		obj.userType = 2;
		// 	} else if(this.$global.user.userTypeId == 1) {
		// 		obj.account = this.$global.user.oaNum;
		// 		obj.userType = 1;
		// 	}
		// 	var that = this;

		// 	_Api.getAuthInfo(obj).then(function(response) {
		// 		var data = response.data;
		// 		if(data.code == "1") {
		// 			that.comOptions = data.rows.myMstCompany;
		// 			that.cusOptions = data.rows.myMstSupplier;
		// 			that.authInfo  = that.comOptions;
		// 		} else {
		// 			that.comOptions = [];
		// 			that.cusOptions = [];
		// 		}
		// 	}).catch(() => {
		// 		that.$alert("操作失败！", {
		// 			showClose: false,
		// 			confirmButton: "确定",
		// 			customClass: "alertFailure"
		// 		});
		// 		that.comOptions = [];
		// 		that.cusOptions = [];
		// 	})
		// },
		handleSizeChange (params) {
			this.pageSize = params;
			this.currentPage = 1;
			this.search();
		},
		handleCurrentChange (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		handlePrevClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		handleNextClick (params) {
			if (this.textChange === 1 || this.current === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		addAccountCheck () {
			//新增实物对账
			this.diaShow = false;
			//this.accShow = true;
			this.supplierName = this.$global.user.employeeName + ' (' + this.$global.user.userName + ')';
		},
		del () {
			this.$refs.getAccountInfo.getSelectedRow();
		},
		cancelComponents (msg) {
			if (msg.isCancel == true) {
				//this.accShow = false;
				var that = this;
				this.diaShow = true;
				this.pageLayOut = "add";
				this.signModel = msg.signMode
				if (this.selCompanyCode.id) {
					this.$nextTick(_ => {
						that.search();
					})
				}
			}
		},
		getClickValue (msg) {
			if (msg != "" && msg) {
				this.diaShow = false;
				//this.accShow = true;
				this.pageLayOut = "edit";
				this.setClickValue = msg;//获取查询结果、不只是表格行
			}
		},
		auditAccountData () {
			this.$refs.getAccountInfo.auditSelectedRows();
		},
		abandonAccountData () {
			this.$refs.getAccountInfo.abandonSelectedRows();
		},

		//初始化界面控件
		initControl () {
			if (constant.user.userTypeId == 4) {
				this.btnShow = true;
				this.placeholder = "请选择客户"
				this.label = "客户";
				this.recOptions = [
					{ value: 3, label: "已确认" },
					{ value: 4, label: "已审核" }
				];
				//默认单据状态：提交
				this.recCode = 3;
			} else {
				this.btnShow = false;
				this.placeholder = "请选择公司";
				this.label = "公司";
				this.labelWidth = '70px';
				this.recOptions = [
					{ value: 3, label: "已确认" },
					{ value: 4, label: "已审核" }
				];
				this.recCode = 3;
			}
			//默认日期范围 三个月
			var that = this;
			serviceTime.getServiceTime().then((serviceTime) => {
				if (serviceTime) {
					var resultDate;
					var year = serviceTime.getFullYear();
					var month = serviceTime.getMonth() + 1;
					var month2;
					var dataArr = [];
					dataArr.push(serviceTime.Format("yyyy-MM"));
					var day = 0;
					for (var i = 0; i < 2; i++) {
						switch (month) {
							case 1:
								year--;
								month += 11;
								break;
							default:
								month--;
								break;
						}
						month2 = month;
						month2 = (month2 < 10) ? ('0' + month2) : month2;
						resultDate = year + '-' + month2;
						dataArr.push(resultDate);
					}
					for (let i in dataArr) {
						var data = dataArr[i].split('-');
						day += new Date(data[0], data[1], 0).getDate();
					}
					that.applySDate = new Date(serviceTime.getTime() - 3600 * 1000 * 24 * day).Format('yyyy-MM-dd');
				}
				that.applyEDate = serviceTime.Format("yyyy-MM-dd");
				that.placeholderDate.start = "开始日期"
				that.placeholderDate.end = "结束日期"
				// 设置时间成功后初始化数据
				that.intiData()
			})
		},

		//初始化界面数据源
		intiData () {
			//查询公司列表
			var obj = {};
			if (constant.user.userTypeId == 4) {
				obj.account = constant.user.userName;
				obj.userType = 2;
			}
			else {
				obj.account = constant.user.oaNum;
				obj.userType = 1;
			}
			var that = this;
			_Api.getAuthInfo(obj).then(function (response) {
				var data = response.data;
				if (data.code == "1") {
					that.btnDis = false;
					that.comOptions = data.rows.myMstCompany;
					that.cusOptions = data.rows.myMstSupplier;
					that.authInfo = that.comOptions;
					//默认公司
					if (constant.user.userTypeId != 4) {
						//找登录人所在公司的条目
						for (var i = 0; i < that.comOptions.length; i++) {
							if (that.comOptions[i].companyCode == constant.user.bukrs) {
								that.selCompanyCode = that.comOptions[i];
							}
						}
					}
					else {
						//找第一家公司
						that.selCompanyCode = that.comOptions[0];
					}
					that.layout.companyCode = that.selCompanyCode.companyCode;
					that.layout.auth = true;
					//自动查询
					that.search();
				}
				else {
					that.comOptions = [];
					that.cusOptions = [];
				}

			}).catch((err) => {
				Msg.showErrorMsg(err.data.message);
				that.comOptions = [];
				that.cusOptions = [];
			})
		}
	},
	mounted () {
		//初始化控件
		this.initControl()
	},
	watch: {
		selCompanyCode (value) {
			this.currentPage = 1;
			if (value)
				this.search();
		},
		selCusName () {
			this.currentPage = 1;
			this.search();
		},
		applySDate (value) {
			if (value == null)
				this.applySDate = "";
			this.current = 1;
		},
		applyEDate (value) {
			if (value == null)
				this.applyEDate = "";
			this.current = 1;
		},
		accountNo (val) {
			if (val)
				this.textChange = 1;
			else
				this.textChange = 0;
		}
	}
}
</script>

<style>
</style>